const { customersModel } = require('./models/customersModels');
const { salesmansModel } = require('./models/salesmansModel')

//新增用户
module.exports.addCustomers = async customersData => {
    return await customersModel.create(customersData);
}

//获取所有用户
module.exports.getCustomers = async ({ type = "name", value = "", onePageData, nowPage }) => {
    const total = await customersModel.countDocuments({ delete: false, [type]: { $regex: value, $options: '$i' } });
    const pages = Math.ceil(total / onePageData);

    const data = await customersModel
        .find({ delete: false, [type]: { $regex: value, $options: '$i' } })
        // .populate('classesId')
        //多表关联查询
        .populate({
            path: 'salesmansId'
        })
        //设置每页显示多少条数据
        .limit(onePageData - 0)
        //设置跳过多少条数据显示
        .skip((nowPage - 1) * onePageData)
    return { data, total, pages };
}

//通过销售员Id获取用户
module.exports.getCustomersBySalesmansId = async idData => {
    // console.log(idData);
    return await customersModel.find({ delete: false, ...idData })
}

//删除用户
module.exports.deleteCustomers = async idData => {
    return await customersModel.updateOne(idData, { delete: true });
}

//通过id获取用户数据
// module.exports.alterCustomers = async idData => {
//     return await customersModel.find(idData);
// }

//修改用户数据
module.exports.sureAlterCustomers = async alterData => {
    return await customersModel.updateOne({ _id: alterData._id }, alterData);
}


//个人门户登录注册验证是否存在帐号
module.exports.getUsers = async usersData => {
    return await customersModel.find(usersData)
}